package com.zhongdu.stady.controler;


import com.zhongdu.stady.pojo.User;
import com.zhongdu.stady.service.IUserService;
import com.zhongdu.stady.shiro.ELoginType;
import com.zhongdu.stady.token.PhoneToken;
import com.zhongdu.stady.vo.PhoneValidCodeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * s
 */
@RequestMapping("user/")
@Controller
@Slf4j
public class UserController {
//    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    private IUserService userService;
    /**
     * 用户名密码登录
     * @param user
     * @return
     */
//    @PostMapping("loginByusername")
//    @ResponseBody
//    public String loginByUsername(User user){
//        log.info(user.toString());
//        PhoneToken token = new PhoneToken(ELoginType.USERNAME,user.getUsername(),user.getPassword());
//        return shiroLogin(token);
//    }


    @RequestMapping(value = "loginByusername")
    public String login(@RequestParam String username, @RequestParam String password, Model model){
        log.info("username:["+username+"}   password:{"+password+"]");
        try {
            model.addAttribute("username", username);
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
            Subject subject = SecurityUtils.getSubject();
            //完成登录
            subject.login(usernamePasswordToken);
            return "redirect:/index.html";
        } catch (Exception e) {
            String ex = e.getClass().getName();
            if (ex != null) {
                if (UnknownAccountException.class.getName().equals(ex)) {
                    System.out.println("用户名不存在");
                } else if (IncorrectCredentialsException.class.getName().equals(ex)) {
                    System.out.println("账户或密码错误");
                } else {
                    System.out.println("未知错误");
                }
            }
            //返回登录页面
            return "redirect:/login.html";
        }
    }
    public String index(){
        return "redirect:/index.html";
    }
    /**
     * 手机验证码登录
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("loginByPhone")
    public String loginByPhone(PhoneValidCodeVo user){
        log.info(user.toString());
        PhoneToken token = new PhoneToken(ELoginType.PHONE,user.getPhone()+"",user.getValidCode()+"");
        return shiroLogin(token);
    }
    public String shiroLogin(PhoneToken token){
        try {
            //登录不在该处处理，交由shiro处理
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            return "登录成功";
        }catch (Exception e){
            log.info("",e);
            return "登录失败";
        }
    }


    /**
     * 查询用户列表
     * @return
     */
    @ResponseBody
    @RequestMapping("selectUsers")
    public List<User> selectUsers(){
        log.info("开始查询");
        List<User> list = userService.selectUsers();
        log.info(list.toString());
        return list;
    }
}
